const UserService = require("../../services/admin/user.service");
const JWT = require("../../utils/JWT");
require("../../utils/JWT");
const UserController = {
  login: async (req, res) => {
    // 数据库增删改查操作
    const result = await UserService.login(req.body);
    if (result) {
      const token = JWT.generate({ result }, "30d");
      const { username, role, gender, _id, avatar, intr } = result;
      res.header("token", token);
      res.send({
        ActionType: "OK",
        data: { username, role, gender, _id, avatar, intr },
      });
    } else {
      res.send({
        code: "-1",
        error: "用户名或密码错误",
      });
    }
  },
  update: async (req, res) => {
    const { username, gender, intr, _id } = req.body;
    const avatar = `/uploads/${req.file.filename}`;
    const da = await UserService.upload({
      username,
      gender: Number(gender),
      intr,
      _id,
      avatar,
    });
    console.log(da);
    res.send({
      ActionType: "OK",
    });
  },
};

module.exports = UserController;
